// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: leon
#pragma once

#include <iostream>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <memory>

#include <boost/date_time/posix_time/posix_time.hpp>

#include "coin2/exchange/base/symbology/derivative_util.h"
#include "coin2/exchange/base/symbology/futures_product.h"
#include "coin2/exchange/mexc_futures/symbology/currency.h"

namespace coin2::exchange::mexc_futures::symbology {
using ExchangeType = base::market::ExchangeType;
using coin2::exchange::mexc_futures::symbology::MexcFuturesCurrency;

class MexcFuturesProduct
    : public base::symbology::FuturesProduct<MexcFuturesCurrency, MexcFuturesProduct>,
      public base::symbology::ProductFromStrMixin<MexcFuturesProduct> {
 public:
  using FuturesProduct = base::symbology::FuturesProduct<
      MexcFuturesCurrency, MexcFuturesProduct>;
  using ContractType = base::symbology::ContractType;

 public:
  MexcFuturesProduct(
      const MexcFuturesCurrency& base,
      const MexcFuturesCurrency& quote,
      const std::string& native_product,
      const std::string& native_subscription_symbol,
      const ContractType contract_type,
      const int64_t expiry_timestamp,
      const int64_t resolve_timestamp)
      : FuturesProduct(
            base,
            quote,
            native_product,
            native_subscription_symbol,
            base::market::ExchangeType::Mexc,
            contract_type,
            expiry_timestamp,
            resolve_timestamp) {}

 public:
  static std::unique_ptr<MexcFuturesProduct> FromNativeStrImpl(
      const std::string& native_product,
      const int64_t resolve_timestamp) {
    std::vector<std::string> out;
    std::unique_ptr<MexcFuturesCurrency> base;
    std::unique_ptr<MexcFuturesCurrency> quote;
    std::vector<std::string> splitted;
    splitted = Split(native_product, "_");
    if (splitted.size() != 2) {
      return {};
    }
    base = MexcFuturesCurrency::FromNativeStrNoThrow(splitted[0], resolve_timestamp);
    if (!base) {
      return {};
    }
    quote = MexcFuturesCurrency::FromNativeStrNoThrow(splitted[1], resolve_timestamp);
    if (!quote) {
      return {};
    }
    std::string expiry_str = "PERPETUAL";
    std::string native_product_tmp;
    std::string native_subscription_symbol;
    ContractType contract_type;
    int64_t expiry_timestamp;
    bool success = CalculateParams(
        *base, *quote, expiry_str, resolve_timestamp,
        &native_product_tmp, &native_subscription_symbol,
        &contract_type, &expiry_timestamp);
    if (success) {
      DCHECK_EQ(native_product_tmp, native_product);
      return std::make_unique<MexcFuturesProduct>(
          *base, *quote, native_product, native_subscription_symbol,
          contract_type, expiry_timestamp, resolve_timestamp);
    } else {
      return {};
    }
  }

  static bool CalculateParams(
      const MexcFuturesCurrency& base,
      const MexcFuturesCurrency& quote,
      const std::string& expiry_str,
      const int64_t resolve_timestamp,
      std::string* native_product,
      std::string* native_subscription_symbol,
      ContractType* contract_type,
      int64_t* expiry_timestamp) {
    using base::symbology::ContractType;
    using base::symbology::StringToContractType;
    using base::symbology::IsDateStr;
    using base::symbology::FindExpiryFriday;
    using base::symbology::ToDateStr;

    if (IsDateStr(expiry_str)) {
      return false;
    } else {
      auto type = StringToContractType(expiry_str);
      if (type) {
        *contract_type = type.value();
        if (*contract_type == ContractType::PERPETUAL) {
          *expiry_timestamp = FuturesProduct::PERPETUAL_TIMESTAMP;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }

    std::stringstream ss;
    ss << base.native_currency() << "_" << quote.native_currency();
    *native_product = ss.str();
    *native_subscription_symbol = *native_product;
    return true;
  }
};

}  // namespace coin2::exchange::mexc_futures::symbology
